const baseUrl = 'http://localhost:8081'

export default {
	baseUrl,
	request: function request(options) {
		const {
			url,
			data,
			header = {},
			method = 'GET'
		} = options;
		const token = uni.getStorageSync('token') || '';
		const requestHeader = {
			'content-type': 'application/json',
			Authorization: token,
			...header
		};
		return new Promise((resolve, reject) => {
			var finalUrl = baseUrl + url;
			uni.request({
				url: finalUrl,
				data,
				header: requestHeader,
				method,
				success: res => {
					const {
						statusCode,
						data: responseData
					} = res;
					if (statusCode === 200) {
						resolve(responseData);
					} else {
						reject(res);
					}
				},
				fail: err => {
					reject(err);
				}
			});
		});
	},
	post:function post(url, data) {
		const token = uni.getStorageSync('token') || '';
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data,
				header: {
					'content-type': 'application/json',
					Authorization: token
				},
				method: 'POST',
				success: res => {
					const {
						statusCode,
						data: responseData
					} = res;
					if (statusCode === 200) {
						resolve(responseData);
					} else {
						reject(res);
					}
				},
				fail: err => {
					reject(err);
				}
			});
		});
	},
	get: function get(url, data) {
		const token = uni.getStorageSync('token') || '';
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				data,
				header: {
					'content-type': 'application/json',
					Authorization: token
				},
				method: 'GET',
				success: res => {
					const {
						statusCode,
						data: responseData
					} = res;
					if (statusCode === 200) {
						resolve(responseData);
					} else {
						reject(res);
					}
				},
				fail: err => {
					reject(err);
				}
			});
		});
	}
}